<template>
  <div class="table-box">
    <div class="table-search">
      <el-form :inline="true">
        <el-form-item label="买入总计：">
          {{ total.buy_total_amount }} {{ total.buy_currency }}
        </el-form-item>
        <el-form-item label="卖出总计：">
          {{ total.sell_total_amount }} {{ total.sell_currency }}
        </el-form-item>
      </el-form>
    </div>
    <div class="table-search">
      <el-form :inline="true" :model="form">
        <el-form-item>
          <el-select
            clearable
            v-model="form.merchant_id"
            placeholder="请选择商户"
            filterable
          >
            <el-option
              v-for="item in merchantList"
              :key="item.value"
              :label="item.name + '-' + item.merchant_id"
              :value="item.merchant_id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <!-- <el-form-item>
          <el-input clearable v-model="form.merchant_id" placeholder="商户ID" />
        </el-form-item>
        <el-form-item>
          <el-input
            clearable
            placeholder="商户名称"
            v-model="form.merchant_name"
          ></el-input>
        </el-form-item> -->
        <el-form-item>
          <el-input
            clearable
            v-model="form.order_no"
            placeholder="订单流水号"
          />
        </el-form-item>
        <el-form-item>
          <el-select
            v-model="form.buy_currency"
            placeholder="买入币种"
            clearable
            style="width: 120px"
          >
            <el-option
              v-for="item in currencyList"
              :key="item.name"
              :label="item.alias"
              :value="item.name"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-select
            v-model="form.sell_currency"
            placeholder="卖出币种"
            clearable
            style="width: 120px"
          >
            <el-option
              v-for="item in currencyList"
              :key="item.name"
              :label="item.alias"
              :value="item.name"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-select
            v-model="form.status"
            placeholder="订单状态"
            style="width: 120px"
          >
            <el-option
              v-for="item in statusList"
              :key="item.type"
              :label="item.name"
              :value="item.type"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-date-picker
            v-model="form.timeData"
            type="datetimerange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="截止日期"
            :default-time="pickerDefaultTime"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onQuery">查询</el-button>
          <el-button @click="onReset">重置</el-button>
          <el-button type="primary" plain @click="onExport">导出</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="card content-box">
      <div class="table">
        <el-table :data="tableData" border v-loading="loading">
          <el-table-column prop="id" label="ID" align="center" />
          <el-table-column
            prop="order_no"
            label="订单流水号"
            align="center"
            width="260"
          />
          <el-table-column
            prop="merchant_id"
            label="商户ID"
            align="center"
            width="110"
          />
          <el-table-column
            prop="merchant_name"
            label="商户名称"
            align="center"
            width="150"
          />
          <el-table-column prop="sell_amount" label="卖出金额" align="center" />
          <el-table-column
            prop="sell_currency"
            label="卖出币种"
            align="center"
          />
          <el-table-column prop="buy_amount" label="买入金额" align="center" />
          <el-table-column
            prop="buy_currency"
            label="买入币种"
            align="center"
          />
          <el-table-column prop="price" label="卖出币价格" align="center" />
          <el-table-column prop="o_price" label="买入币价格" align="center" />
          <el-table-column prop="status" label="状态" align="center">
            <template #default="scope">
              <el-tag
                :type="
                  scope.row.status == 1
                    ? 'success'
                    : scope.row.status == 2
                    ? 'danger'
                    : 'warning'
                "
              >
                {{
                  scope.row.status == 1
                    ? "已完成"
                    : scope.row.status == 2
                    ? "已取消"
                    : "处理中"
                }}
              </el-tag>
            </template>
          </el-table-column>
          <el-table-column
            prop="exchange_time"
            label="换汇时间"
            align="center"
            :formatter="formatterTime"
            width="180"
          />
          <el-table-column
            prop="createtime"
            label="订单创建时间"
            align="center"
            :formatter="formatterTime"
            width="180"
          />
        </el-table>
        <div class="table-pagination">
          <el-pagination
            :current-page="pageable.pageNum"
            :page-size="pageable.pageSize"
            :page-sizes="[10, 25, 50, 100]"
            :background="true"
            layout="total, sizes, prev, pager, next, jumper"
            :total="pageable.total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          ></el-pagination>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted, reactive, computed } from "vue";
import {
  formatterTime,
  timestamp,
  pickerDefaultTime,
  exportOrDownload,
} from "@/utils/utils";
import { exchangeExOrderApi } from "@/api/payServerse/exchange";
import { assetsCurrencyApi } from "@/api/payServerse/settings";
import { GlobalStore } from "@/store";

const globalStore = GlobalStore();
const merchantList = computed(() => globalStore.merchantList);
const loading = ref(false);
const tableData = ref([]);
const currencyList = ref([]); //币种
const formData = {
  buy_currency: "",
  sell_currency: "",
  merchant_id: "",
  merchant_name: "",
  order_no: "",
  status: "",
  timeData: [],
};
const form = reactive({ ...formData });
const statusList = [
  { type: "0", name: "处理中" },
  { type: "1", name: "已完成" },
  { type: "2", name: "已取消" },
];
const pageable = reactive({
  pageNum: 1,
  pageSize: 10,
  total: 0,
});
const total = reactive({
  buy_total_amount: 0,
  buy_currency: "",
  sell_total_amount: 0,
  sell_currency: "",
});
// 参数
const param = () => {
  return {
    page: pageable.pageNum,
    pagesize: pageable.pageSize,
    merchant_id: form.merchant_id,
    merchant_name: form.merchant_name,
    order_no: form.order_no,
    status: form.status,
    buy_currency: form.buy_currency,
    sell_currency: form.sell_currency,
    start: form.timeData[0] ? timestamp(form.timeData[0].valueOf()) : "",
    end: form.timeData[1] ? timestamp(form.timeData[1].valueOf()) : "",
  };
};
// 获取币种
const getCurrencyList = async () => {
  try {
    const res = await assetsCurrencyApi({});
    currencyList.value = res.data.results;
  } catch (error) {
    currencyList.value = [];
  }
};
// 获取表格数据
const getTbaleData = async () => {
  loading.value = true;
  try {
    const res = await exchangeExOrderApi(param());
    loading.value = false;
    tableData.value = res.data.results;
    pageable.total = res.data.count;
    total.buy_currency = res.data.extra.total?.buy_currency;
    total.buy_total_amount = res.data.extra.total?.buy_total_amount;
    total.sell_currency = res.data.extra.total?.sell_currency;
    total.sell_total_amount = res.data.extra.total?.sell_total_amount;
  } catch (error) {
    loading.value = false;
    return false;
  }
};
// 导出
const onExport = async () => {
  const data = Object.assign(param(), { export: 1, pagesize: 9999999 });
  const res = await exchangeExOrderApi(data, "blob");
  exportOrDownload(res, "EX-换汇记录");
};
// 重置
const onReset = () => {
  Object.assign(form, formData);
};
// 查询
const onQuery = async () => {
  getTbaleData();
};
// 条数
const handleSizeChange = (number) => {
  pageable.pageSize = number;
  getTbaleData();
};
// 页码
const handleCurrentChange = (number) => {
  pageable.pageNum = number;
  getTbaleData();
};
onMounted(() => {
  getTbaleData();
  getCurrencyList();
});
</script>
